﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vITs.Site.Models.DataModels;

namespace vITs.Site.Controllers
{
    [Authorize(Roles = "Administratör")]
    public class PerDiemController : Controller
    {
        [HttpGet]
        public ActionResult Index(Int32 Year = -1)
        {
            if (Year == -1) Year = DateTime.Now.Year;

            var pds = new VitsDB().PerDiemSpecifications.Where(p => p.Year == Year);
            this.ViewBag.Year = Year.ToString();

            return View(pds);
        }

        [HttpGet]
        public ActionResult Create()
        {
            return this.View();
        }

        [HttpPost]
        public ActionResult Create(PerDiemSpecification pds)
        {
            if (this.ModelState.IsValid)
            {
                VitsDB db = new VitsDB();
                bool anyErrors = false;
                string country = pds.Country.Trim().ToUpper();

                if (String.IsNullOrEmpty(country))
                {
                    anyErrors = true;
                    this.ModelState.AddModelError("Country", "Du måste ange ett land");
                }

                if (pds.Year < 1900 || 2500 < pds.Year)
                {
                    anyErrors = true;
                    this.ModelState.AddModelError("Year", "Året måste vara mellan år 1900 och år 2500");
                }

                if (pds.Sum < 198)
                {
                    anyErrors = true;
                    this.ModelState.AddModelError("Sum", "Den lägsta traktamentessumman är 198.");
                }
                

                if (!anyErrors)
                {
                    db.AddToPerDiemSpecifications(pds);
                    if (db.SaveChanges() > 0)
                    {
                        return this.RedirectToAction("Index", new { Year = pds.Year });
                    }
                    else
                    {
                        this.ModelState.AddModelError("", "Kunde inte spara specifikationen.");
                    }
                }
            }

            return this.View(pds);
        }

        [HttpGet]
        public ActionResult Edit(Int32 Year, String Country)
        {
            PerDiemSpecification pds = new VitsDB().PerDiemSpecifications.SingleOrDefault(p => p.Year == Year && p.Country == Country);
            if (pds == null) return this.RedirectToAction("Index", new { Year = Year });

            return this.View(pds);
        }

        [HttpPost]
        public ActionResult Edit(PerDiemSpecification pds)
        {
            if (this.ModelState.IsValid)
            {
                if (pds.Sum >= 198)
                {
                    VitsDB db = new VitsDB();
                    db.PerDiemSpecifications.SingleOrDefault(p => p.Year == pds.Year && p.Country == pds.Country.ToUpper()).Sum = pds.Sum;
                    db.SaveChanges();

                    return this.RedirectToAction("Index", new { Year = pds.Year });
                }
                else
                {
                    this.ModelState.AddModelError("Sum", "Den lägsta traktamentessumman är 198.");
                }
            }

            return this.View(pds);
        }

        [HttpGet]
        public ActionResult Delete(Int32 Year, String Country)
        {
            VitsDB db = new VitsDB();
            var pds = db.PerDiemSpecifications.SingleOrDefault(p => p.Year == Year && p.Country == Country);

            if (pds != null)
            {
                db.PerDiemSpecifications.DeleteObject(pds);
                db.SaveChanges();
            }

            return this.RedirectToAction("Index", new { Year = pds.Year });
        }
    }
}
